package com.qingke.pms.app;

import com.qingke.pms.command.Command;
import com.qingke.pms.factory.CommandFactory;
import com.qingke.pms.factory.CommandFactory.CommandCode;
import com.qingke.pms.pojo.Project;
import com.qingke.pms.pojo.User;
import com.qingke.pms.util.PMSConsole;

public class ProjectManagementSystemApplication {
	private static ProjectManagementSystemApplication instance=null;
	private User user=null;
	private Project project=null;
	private boolean isRun=true;
//	private String promptStr;
	private ProjectManagementSystemApplication(){
		
	}
	
	public static ProjectManagementSystemApplication getInstance(){
		if(instance==null){
			instance=new ProjectManagementSystemApplication();
		}
		return instance;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setRun(boolean isRun) {
		this.isRun = isRun;
	}
	
	public void setProject(Project project){
		this.project=project;
	}
	
	public void run(){
		PMSConsole.println("Welcome");
		String cmd;
		while(isRun){
			cmd=PMSConsole.prompt("cmd>").trim().replaceAll(" ","").toUpperCase();
			
			CommandCode cmdCode = null;
			try{
				cmdCode=CommandCode.valueOf(cmd);
			}catch(Exception e){
				PMSConsole.println("err input,you can input 'help' to look command.");
				
				continue;
			}
			
			CommandFactory factory = CommandFactory.getFactory(user,project); 
			Command command = factory.buildCommand(cmdCode);
			if(command!=null){
				command.execute();
				continue;
			}
			PMSConsole.println("err input,you can input 'help' to look command.");
			
		}
	}
}
